#include "portal.h"

Portal::Portal(Image* open_img, Image* closed_img, Image* walledImg)
{
	_isSet = false;
	_sprite.SetImage(closed_img);
	
	_portalReadyForEntry = true;
	
	_openPortalImg = open_img;
	_closedPortalImg = closed_img;
	_walledImg = walledImg;
	
	_walled = false;
	_open = false;
}

Portal::~Portal()
{

}

void Portal::SetWalled(bool b)
{
	if(b)
	{
		_sprite.SetImage(_walledImg);
	}
	else if(_open)
	{
		_sprite.SetImage(_openPortalImg);
	}
	else
	{
		_sprite.SetImage(_closedPortalImg);
	}
	
	_walled = b;
}
	
void Portal::Render(float dx, float dy)
{
	if(!_walled)
	{
		_Rotation += 3;
		_Rotation = ((int)_Rotation % 360);

		if(_isSet)
		{
			_sprite.SetPosition(_Location.x, _Location.y);
			_sprite.SetRotation(_Rotation/2.0);
		}
	}

	if(_open)
	{
		_sprite.SetImage(_openPortalImg);
		if(_isSet)
		{
			_sprite.SetPosition(_Location.x, _Location.y);
			_sprite.Draw(dx, dy);
		}
	}
	else
	{
		_sprite.SetImage(_closedPortalImg);
		if(_isSet)
		{
			_sprite.SetPosition(_Location.x, _Location.y);
			_sprite.Draw(dx, dy);
		}
	}
}

void Portal::SetAngle(float a)
{
	_sprite.SetRotation(a);
	
	_angle = (a*2.0) + 90.0;
}

float Portal::IsInPortal(Sprite* s)
{
	if(_sprite.CollidesWith(s, true))
	{
		if(!_portalReadyForEntry)
		{
			return 0;
		}
		else
		{
			_portalReadyForEntry = false;
			return 1;
		}
	}
	
	_portalReadyForEntry = true;
	return 0;
}
